<template>
  <div>
    <el-page-header @back="goBack"> </el-page-header>

    <el-card style="min-width: 1200px">
      <div class="createWebhooks">
        <img src="@/assets/imgs/setting/add.png" class="img" />
        <div>
          <div class="title">Webhooks测试</div>
          <div class="hint">Webhooks接收</div>
          <el-select value="测试URL" placeholder="请选择" disabled>
            <el-option label="http://" value="1"></el-option>
            <el-option label="https://" value="2"></el-option>
          </el-select>
          <el-input
            disabled
            v-model="row.url"
            style="width: 700px; margin-left: 20px"
          ></el-input>
        </div>
      </div>
      <el-row style="margin-top: 30px">
        <el-col :span="8">
          <el-table
            :data="dataList"
            :row-class-name="tableRowClassName"
            ref="singleTable"
          >
            <template slot="empty">
              <el-empty
                :image="require('@/assets/imgs/middleWork/k1.png')"
                :image-size="80"
                description="暂无相关数据"
              ></el-empty>
            </template>
            <el-table-column
              show-overflow-tooltip
              prop="eventName"
              align="center"
              label="事件类型"
            >
            </el-table-column>
            <el-table-column
              show-overflow-tooltip
              prop="testResult"
              align="center"
              label="测试结果"
            >
            </el-table-column>
            <el-table-column
              show-overflow-tooltip
              prop="act"
              align="center"
              label="操作"
            >
              <template slot-scope="scope">
                <el-button @click="handleClick(scope.row)" type="text"
                  >测试发送</el-button
                >
                <el-button
                  v-if="scope.row.testResult"
                  @click="setCurrent(scope.row)"
                  type="text"
                  >查看结果</el-button
                >
              </template>
            </el-table-column>

            <!-- <json-viewer
                v-if="scope.row.sendData"
                :value="JSON.parse(scope.row.sendData)"
              ></json-viewer> -->
            <!-- <el-table-column show-overflow-tooltip prop="sendData" label="云尘付发送的请求">
            <template>
              
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip
            prop="sendResultCode"
            align="center"
            width="150"
            label="你的服务器响应"
          >
          </el-table-column> -->
          </el-table>
        </el-col>
        <el-col :span="16">
          <el-row>
            <el-col :span="12" style="text-align: center">
              <div class="t_title">云尘付发送的请求</div>
              <pre class="pre">
                 {{ textarea ? textarea : "" }}
                </pre
              >
            </el-col>
            <el-col :span="12" style="text-align: center">
              <div class="t_title">你的服务器响应返回</div>
              <pre class="pre">
                 {{ resultText ? resultText : "" }}

                </pre
              >
              <!-- <div class="result_css">{{resultText}}</div> -->
            </el-col>
          </el-row>
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>

<script>
import base64 from "@/utils/code";

export default {
  name: "settingWebhooksTest",
  data() {
    return {
      rowItem: {},
      dataList: [],
      dataCopy: [],
      row: {},
      textarea: "",
      resultText: "",
    };
  },
  created() {
    this.row = JSON.parse(base64.decode(this.$route.query.item)) || {};
    this.row.url = this.row.urlAgreement + "://" + this.row.notifyUrl;
    this.getList();
  },
  watch: {
    "$store.state.testLine": {
      handler() {
        if(this.$route.name==this.$options.name){
          this.$router.push("/setting/webhooks");
        }
      },
    },
    deep: true,
  },
  methods: {
    tableRowClassName({ row }) {
      if (this.rowItem == row) {
        return "success-row";
      }
      return "";
    },
    setCurrent(row) {
      this.rowItem = row;
      this.textarea = row.textarea;
      this.resultText = row.resultText;
    },
    getList() {
      const url = this.row.notifyUrl;
      this.$http
        .get(
          `/api/webhooks/getTestDatas?url=${url}`,
        )
        .then((res) => {
          if (res.code !== "0000") {
            this.dataList = [];
            //   return this.$message.error(res.msg);
          }
          this.dataList = res.data;
          if (this.dataList) {
            this.dataList.forEach((e) => {
              if (e.dataId == this.rowItem.dataId) {
                this.setCurrent(e);
              }
            });
          }
        })
        .catch(() => {
          // this.dataListLoading = false
        });
    },
    goBack() {
      this.$backTo(`/setting/webhooks`);
    },
    objectSpanMethod({ columnIndex }) {
      if (columnIndex === 3 || columnIndex === 4) {
        return {
          rowspan: this.dataList.length,
          colspan: 1,
        };
      }
    },
    handleClick(obj) {
      this.rowItem = obj;
      const url = this.row.urlAgreement + "://" + this.row.notifyUrl;
      this.$http
        .post("/api/webhooks/resendTest", {
          dataId: obj.dataId,
          eventType: obj.eventType,
          // sendData: this.textarea,
          url,
        })
        .then((res) => {
          if (res.code === "0000") {
            this.textarea = res.data.requestParam;
            this.resultText = res.data.responseParam;
            this.dataList.forEach((e) => {
              if (e.eventType == res.data.event_type) {
                e.testResult = res.data.result;
                e.textarea = res.data.requestParam;
                e.resultText = res.data.responseParam;
              }
            });
          } else {
            this.textarea = "";
            this.resultText = "";
          }
        });
    },
  },
};
</script>
<style>
.el-table .success-row {
  background: #e5f8f9;
}
</style>
<style lang="scss" scoped>
.createWebhooks {
  display: flex;
  padding: 20px;
  .img {
    width: 86px;
    height: 86px;
    margin-right: 40px;
  }
  .title {
    font-size: 22px;
    font-weight: 600;
    color: #4f595f;
    margin-bottom: 5px;
  }
  .hint {
    color: #869daa;
    margin-bottom: 32px;
  }
}
.t_title {
  height: 50px;
  line-height: 50px;
  font-size: 14px;
  font-weight: 600;
  background: rgb(244, 248, 251);
  color: #909399;
  text-align: center;
  margin-bottom: 8px;
}
.pre {
  white-space: pre-line; /*保留换行符，设置溢出换行*/
  text-align: left;
  width: 98%;
  height: 500px;
  overflow: auto;
  border: 1px solid #90939960;
}
.result_css {
  text-align: center;
}
:deep(.el-select) {
  .el-input__inner {
    width: 118px;
    border: none;
    background: #e5f8f9;
    color: #869daa;
  }
  .el-icon-arrow-up {
    &::before {
      content: "\e78f";
      color: #889fab;
    }
  }
  .el-input__suffix {
    opacity: 0;
  }
}
</style>
